package com.etop.ondoc.util;

import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class Thumb {

    /**
     * 注意，覆盖原图片<br/>
     *
     * @param path:原图片路径
     * @return 缩略图地址
     */
    public static String thumbnailsProductor(String path) {
        try {
            AffineTransform transform = new AffineTransform();//2D 仿射变换
            File file = new File(path);
            BufferedImage bis = ImageIO.read(file); //读取图片

            //根据原图片的长宽按比例设定缩略图的长宽
            int nw = 400;
            int w = bis.getWidth();
            int h = bis.getHeight();
            int nh = (nw * h) / w;
            double sx = (double) nw / w;
            double sy = (double) nh / h;
            transform.setToScale(sx, sy);
            AffineTransformOp ato = new AffineTransformOp(transform, null);
            BufferedImage bid = new BufferedImage(nw, nh, BufferedImage.TYPE_3BYTE_BGR);//8 位 RGB 颜色分量的图像
            ato.filter(bis, bid);
            
            //生成图片
            ImageIO.write(bid, "jpeg", file);
        } catch (IOException ex) {
            System.out.println("出现图片转换异常：" + ex.getMessage());
        }
        return path;
    }
}
